package modelLayer;

import java.io.IOException;
import java.util.ArrayList;

import uiLayer.SaveToFile;

public class ContractorRecords {
	
	private ArrayList<Contractor> contractors;
	private static ContractorRecords instance;
	public ContractorRecords(){
		contractors = new ArrayList<Contractor>();
	}
	public static ContractorRecords getInstance(){
		if(instance == null){
			instance = new ContractorRecords();
		}
		return instance;
	}
	
	public ArrayList<Contractor> getContractors() {
		return contractors;
	}
	public Contractor findContractor(String id){
		for(Contractor contractor:contractors){
			if (contractor.getId().equals(id)){
				return contractor;
			}
		}
		return null;

	}
	public String getContractor(String id){
		Contractor c1 = findContractor(id);
		return c1.toString();
	}
	public void createContractor(String id, String name, String address,
			String phoneNr) {
		Contractor c1 = new Contractor(id,name,address,phoneNr);
		contractors.add(c1);
		
	}
	public void updateContractor(String id, String address, String phoneNr) {
		for(Contractor c1: contractors){
			if(c1.getId().equals(id)){
				if(!address.equals(null))c1.setAddress(address);
				
				if(!phoneNr.equals(null))c1.setPhoneNr(phoneNr);
			}
		}
	}
	public void deleteContractor(String id) {
		Contractor c1 = findContractor(id);
				contractors.remove(c1);
			}
	public void saveContr() throws IOException{
		for(Contractor c1: contractors){
			SaveToFile.getInstance().save(c1.save());
			
		}
	}
	
}
